﻿using System;
using System.Collections.Generic;
using System.Collections;
using Froggy.Stocks.Analysis.Candles;
using Froggy.Stocks.Analysis.Crawlers;

namespace Froggy.Stocks.Analysis
{
    public static class AnalysisEngine
    {
        private static readonly List<Type> _UnitTypes;

        static AnalysisEngine()
        {
            _UnitTypes = new List<Type>
                             {
                                 typeof (DojiAnalysis),
                                 typeof (AvgArithmeticValueAnalysis)
                             };
        }

        public static IPattern[] Run(IEnumerable quotations, int period)
        {
            var analysisUnits = InstanciateAnalysisUnits(period);
            var patterns = new List<IPattern>();
            foreach (IQuotationSnapshot q in quotations)
            {
                foreach (var analysisUnit in analysisUnits)
                {
                    var pattern = analysisUnit.Analyse(q);
                    if (pattern != null)
                    {
                        patterns.Add(pattern);
                    }
                }
            }
            return patterns.ToArray();
        }

        private static IAnalysis[] InstanciateAnalysisUnits(int period)
        {
            var units = new List<IAnalysis>();
            foreach (var unitType in _UnitTypes)
            {
                var analysisUnit = (IAnalysis)Activator.CreateInstance(unitType);
                analysisUnit.Period = period;
                units.Add(analysisUnit);
            }
            return units.ToArray();
        }
    }
}